package com.marakana.yamba1;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Application;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.SQLException;
import android.preference.PreferenceManager;
import android.util.Log;

import com.chris.bean.Person;
import com.chris.weibo.MainActivity;
import com.marakana.yamba4.DbHelper;
import com.marakana.yamba4.StatusData;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI.FEATURE;
import com.weibo.sdk.android.net.RequestListener;

public class YambaApplication1 extends Application implements
		OnSharedPreferenceChangeListener, RequestListener {
	private static final String TAG = YambaApplication1.class.getSimpleName();
	public Person person;
	private SharedPreferences prefs;
	public SharedPreferences getPrefs() {
		return prefs;
	}

	private StatusData statusData;

	public StatusData getStatusData() {
		return statusData;
	}

	private boolean serviceRunning;

	public boolean isServiceRunning() {
		return serviceRunning;
	}

	public void setServiceRunning(boolean serviceRunning) {
		this.serviceRunning = serviceRunning;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
		this.prefs.registerOnSharedPreferenceChangeListener(this);
		statusData = new StatusData(this);
		Log.i(TAG, "OnCreate");
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		Log.i(TAG, "OnTerminate");
	}

	public synchronized Person getPerson() {
		Person person = new Person();
		person.setUsername(this.prefs.getString("username", "null"));
		person.setPassword(this.prefs.getString("password", "null"));
		person.setApiRoot(this.prefs.getString("apiRoot",
				"http://yamba.marakana.com/api"));
		return person;
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		this.person = null;
	}

	public synchronized int fetchStatusUpdates() {

		Log.d(TAG, "Fetach status updates");

		StatusesAPI statusApi = new StatusesAPI(MainActivity.accessToken);
		statusApi.friendsTimeline(0, 0, 50, 1, false, FEATURE.ALL, false, this);

		return 0;
	}

	@Override
	public void onComplete(String response) {

		JSONObject jsObject;
		try {
			jsObject = new JSONObject(response);
			JSONArray array;
			array = jsObject.getJSONArray("statuses");
			ContentValues values = new ContentValues();
			for (int i = 0; i < array.length(); i++) {
				JSONObject child = array.getJSONObject(i);
				values.clear();
				values.put(StatusData.C_ID, child.getString("id"));
				values.put(StatusData.C_CREATED_AT,
						child.getString("created_at"));
				values.put(StatusData.C_TEXT, child.getString("text"));
				values.put(StatusData.C_USER, child.getJSONObject("user")
						.getString("screen_name"));
				//this.getStatusData().insertOrIgnore(values);
				statusData.insertOrIgnore(values);

				Log.d(TAG, String.format("%s: %s", child.getJSONObject("user")
						.getString("screen_name"), child.getString("text")));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onIOException(IOException e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onError(WeiboException e) {
		// TODO Auto-generated method stub

	}
}
